package cn.codesugar.multimedia

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import androidx.core.content.FileProvider
import cn.codesugar.multimedia.databinding.ActivityCameraAlbumTestBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import java.io.File

private const val FILE_PROVIDER = "cn.codesugar.multimedia.fileprovider"
private const val ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE"

class CameraAlbumTestActivity : AppCompatActivity() {

    private var b: ActivityCameraAlbumTestBinding? = null
    private var takePhoto = 1
    private var fromAlbum = 2
    private lateinit var imageUri: Uri
    private lateinit var outputImage: File

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = ActivityCameraAlbumTestBinding.inflate(layoutInflater)
        setContentView(b?.root)
        b?.takePhotoBtn?.setOnClickListener {
            // 创建File对象，用于存储拍照后的图片
            outputImage = File(externalCacheDir, "output_image.jpg")
            if (outputImage.exists()) outputImage.delete()
            outputImage.createNewFile()
            imageUri = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
                FileProvider.getUriForFile(
                    this,
                    FILE_PROVIDER,
                    outputImage
                )
            } else {
                Uri.fromFile(outputImage)
            }
            // 启动相机
            val intent = Intent(ACTION_IMAGE_CAPTURE)
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
            startActivityForResult(intent, takePhoto)
        }
        b?.fromAlbumBtn?.setOnClickListener {
            // 打开文件选择器
            val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            // 指定只显示图片
            intent.type = "image/*"
            startActivityForResult(intent, fromAlbum)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            takePhoto -> {
                if (resultCode == Activity.RESULT_OK) {
                    b?.imageView?.let {
                        // 本地地图无需缓存
                        Glide.with(this)
                            .load(imageUri)
                            .skipMemoryCache(true)
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .into(it)
                    }
                }
            }
            fromAlbum -> {
                if (resultCode == Activity.RESULT_OK) {
                    data?.data?.let { uri ->
                        b?.imageView?.let { imageView ->
                            Glide.with(this)
                                .load(uri)
                                .skipMemoryCache(true)
                                .diskCacheStrategy(DiskCacheStrategy.NONE)
                                .into(imageView)
                        }
                    }
                }
            }
        }
    }
}